﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Linq;
using KC.DataAccess.MV;

namespace KC.Presentation.Mv8CMS
{
	public partial class PostDesigner : Form
	{
		public PostDesigner()
		{
			InitializeComponent();
		}
		public PostDesigner(int PostID)
		{
			PID = PostID;
			InitializeComponent();
			var p = from p2 in dc.Posts
					where p2.ID == PostID
					select p2;
			if (p.Count() != 1)
			{
				MessageBox.Show("Can't lock onto Post "+PostID.ToString());
				return;
			}

			Post p3 = p.First();
			txtTitle.Text = p3.Title;
			lblFriendlyName.Text = p3.FriendlyName;
			txtBody.Text = KC.Business.Text.Encoding.BlogEncodeForStorage(p3.Body);

			foreach (CategoryToPost ctp in p3.CategoryToPosts)
				lbCategories.Items.Add(ctp.Category);

			cbEnabled.Checked = p3.isEnabled;
		}

		protected MV8DataContext dc = new MV8DataContext(System.Configuration.ConfigurationManager.ConnectionStrings["MV8"].ConnectionString);
		protected int PID = -1;

		private void btnFriendlyName_Click(object sender, EventArgs e)
		{
			lblFriendlyName.Text = System.Web.HttpUtility.UrlEncode(txtTitle.Text);
		}

		private void btnSaveContinue_Click(object sender, EventArgs e)
		{
			DoSave();
		}
		private void btnOK_Click(object sender, EventArgs e)
		{
			DoSave();
			Close();
		}
		private void btnCancel_Click(object sender, EventArgs e)
		{
			Close();
		} 

		private void DoSave()
		{
			// Easy stuff
			Post p;
			if (PID > -1) p = (from p2 in dc.Posts where p2.ID == PID select p2).First();
			else p = new Post();
			p.Title = txtTitle.Text;
			p.Body = KC.Business.Text.Encoding.BlogEncodeForStorage(txtBody.Text);
			if (PID == -1) p.Created = DateTime.Now;
			if (string.IsNullOrEmpty(p.FriendlyName) || (lblFriendlyName.Text != System.Web.HttpUtility.UrlEncode(txtTitle.Text) && MessageBox.Show("Update FriendlyName?", "Question", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes))
				p.FriendlyName = lblFriendlyName.Text;
			p.isEnabled = cbEnabled.Checked;
			if (PID == -1) dc.Posts.InsertOnSubmit(p);
			dc.SubmitChanges();
			PID = p.ID;
			
			// Clear Categories and Tags
			dc.CategoryToPosts.DeleteAllOnSubmit(from ct in dc.CategoryToPosts where ct.PID == PID select ct);
			dc.TagToPosts.DeleteAllOnSubmit(from tt in dc.TagToPosts where tt.PID == PID select tt);
			dc.SubmitChanges();

			// Categorize Post
			foreach (object obj in lbCategories.SelectedItems)
			{
				Category cat = (Category)obj;
				CategoryToPost ctp = new CategoryToPost();
				ctp.CID = cat.id;
				ctp.PID = p.ID;
				dc.CategoryToPosts.InsertOnSubmit(ctp);
			}
			dc.SubmitChanges();

			// Tag Post
			KC.Business.Web.MV8.UpdateTags(p, ref dc);
		}

		private void lblFriendlyName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			System.Diagnostics.Process.Start("http://www.kconnolly.net/Post.aspx?Title=" + lblFriendlyName.Text);
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (PID == -1) return;
			var p = from p2 in dc.Posts where p2.ID == PID select p2;
			if (p.Count() != 1) return;
			if (MessageBox.Show("Really delete this Post?", "Question", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
			{
				try
				{
					KC.DataAccess.MV.Utils.FullyDeletePost(PID, ref dc);
					this.DialogResult = DialogResult.Retry;
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.ToString());
				}
				Close();
			}
		}
	}
}
